package net.liteheaven.dtimer

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import net.liteheaven.dtimer.misc.MyJobIntentService
import net.liteheaven.dtimer.timer.DTimerCallback
import net.liteheaven.dtimer.timer.DTimerManager
import net.liteheaven.dtimer.timer.DToken
import net.liteheaven.servicecompat.R

class MainActivity : AppCompatActivity() {

    private var token: DToken? = null
    private var myAdapter: MyAdapter = MyAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        jobIntentService.setOnClickListener { MyJobIntentService.enqueueWork(context = this@MainActivity, work = Intent()) }
        broadcastAlarm.setOnClickListener {
            val token = DTimerManager.addTimer(
                    101,
                    1000,
                    true
            )
        }
        multiple.setOnClickListener {
            val token = DTimerManager.addTimer(
                    102,
                    1000,
                    false
            )
            this@MainActivity.token = token
        }
        stopMultiple.setOnClickListener {
            val t = this@MainActivity.token
            t?.cancel()
        }

        listView.adapter = myAdapter
        myAdapter.notifyDataSetChanged()
        listView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            val a = parent.adapter as MyAdapter
            val value = a.getItem(position) as String
            result.text = value
        }

        DTimerManager.registerCallback(callback)
    }

    override fun onDestroy() {
        super.onDestroy()
        DTimerManager.unregisterCallback(callback)
    }

    private val callback: DTimerCallback = object : DTimerCallback {
        override fun onTimer(timerId: Int) {
            when (timerId) {
                101 -> {
                    XLog.i("101fired!!!")
                }
                102 -> {
                    XLog.i("102fired!!!")
                }
            }
        }
    }

    private class MyAdapter : BaseAdapter() {

        private val content : ArrayList<String> = ArrayList()

        init {
            for (item : Int in 0..99){
                content.add("sb$item")
            }
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var tv: TextView? = if (convertView == null) {
                TextView(parent!!.context)
            } else {
                convertView as TextView
            }

            val content = getItem(position) as String
            tv!!.text = content
            return tv
        }

        override fun getItem(position: Int): Any {
            return content[position]
        }

        override fun getItemId(position: Int): Long {
            return 0
        }

        override fun getCount(): Int {
            return content.size
        }
    }
}
